﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using QuanLyNganHangDe.BLL;
using QuanLyNganHangDe.DAL;
using QuanLyNganHangDe.DAL.Models;
using QuanLyNganHangDe.GUI.DuLieu;
using QuanLyNganHangDe.GUI.HeThong;
using QuanLyNganHangDe.GUI.TacNghiep;

namespace QuanLyNganHangDe.GUI
{
    public static class Program
    {
        #region Thuộc tính
        public static string connectionString = "";
        private static FormMain m_FormMain = null;
        public static FormMain FormMain
        {
            get
            {
                if (m_FormMain == null)
                    m_FormMain = new FormMain();
                return m_FormMain;
            }
        }
        #endregion

        #region Thuộc tính ứng dụng
        private static NguoiSuDung m_NguoiSuDung;
        public static NguoiSuDung NguoiSuDung
        {
            get { return m_NguoiSuDung; }
            set
            {
                m_NguoiSuDung = value;
                Program.FormMain.UpdateNguoiSuDung();
            }
        }

        //private static HocKy m_HocKy = null;
        //public static HocKy HocKy
        //{
        //    get { return m_HocKy; }
        //    set
        //    {
        //        m_HocKy = value;
        //        Program.FormMain.UpdateHocKy();
        //    }
        //}
        #endregion

        public static string DeThiDirectoryPath
        {
            get
            {
                string exeDirPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                string dataDirPath = System.IO.Path.Combine(exeDirPath, "DE");
                //if (System.IO.Directory.Exists(dataDirPath) == false)
                //    System.IO.Directory.CreateDirectory(dataDirPath);
                return dataDirPath;
            }
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            connectionString = FormThietLapChuoiKetNoi.ReadFileBinary("D:\\DB_NHD\\connect.bin");
            if (string.IsNullOrEmpty(connectionString))
                connectionString = FormThietLapChuoiKetNoi.ReadFileBinary(System.IO.Path.Combine(
                     Application.StartupPath, "connect.bin"));
            if (string.IsNullOrEmpty(connectionString))
                connectionString = FormThietLapChuoiKetNoi.ReadFileBinary("C:\\DB_NHD\\connect.bin");
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
            Application.Run(Program.FormMain);
        }

        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            // Xoá các tệp tạm
            try
            {
                System.Diagnostics.Debug.WriteLine("Start delete temporaty files..");
                #region delete file in folder "%AppData%/QLNHD";
                string folderPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "QLNHD");
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(folderPath);
                foreach (var fi in di.GetFiles())
                    try
                    {
                        System.Diagnostics.Debug.WriteLine("Delete file {0}", fi.FullName);
                        fi.Delete();
                        System.Diagnostics.Debug.WriteLine("ok");
                    }
                    catch (Exception exc)
                    {
                        System.Diagnostics.Debug.WriteLine("fail due to {0}", exc.Message);
                    }
                #endregion
                System.Diagnostics.Debug.WriteLine("delete completed");
            }
            catch (Exception exc)
            {
                System.Diagnostics.Debug.WriteLine("Error: " + exc.Message);
            }
        }
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            DialogUtils.ShowError("Xuất hiện lỗi " + e.Exception.Message);
        }
    }
}
